package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Car;
import com.shaoshaohuo.app.utils.ImageLoadUtil;
import com.shaoshaohuo.app.utils.Location2Util;
import com.shaoshaohuo.app.utils.StringUtil;

import java.text.DecimalFormat;
import java.util.List;

public class CarListAdapter<T> extends MyBaseAdapter<Car> {
    private String lat;
    private String lng;
	public CarListAdapter(Context ctx, List<Car> list, boolean flag, String lat, String lng) {
		super(ctx, list, flag);
        this.lat = lat;
        this.lng = lng;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_car, null);
        }

        final CarViewHolder carHolder = CarViewHolder.getHolder(convertView);
        Car car = list.get(pos);

        carHolder.tv_name.setText(car.getCaruserinfo().getRealname());
        carHolder.tv_num.setText(car.getCarnum());
        if ("2".equals(car.getTriptype())) {
            carHolder.tv_wangfan.setVisibility(View.VISIBLE);
            carHolder.tv_zhifa.setVisibility(View.GONE);
        }else{
            carHolder.tv_wangfan.setVisibility(View.GONE);
            carHolder.tv_zhifa.setVisibility(View.VISIBLE);
        }
        ImageLoader.getInstance().displayImage(car.getCarphoto1(), carHolder.iv_picture, ImageLoadUtil.build);
        carHolder.tv_car_type.setText(car.getCarcname());
        carHolder.tv_car_long.setText(car.getCarlong() + "m");
        carHolder.tv_time.setText(car.getUtime());
        if (StringUtil.isNumericEmpty(car.getLat()) || StringUtil.isNumericEmpty(car.getLng())
                || StringUtil.isNumericEmpty(lat) || StringUtil.isNumericEmpty(lng)) {
            carHolder.tv_car_juli.setText("- -");
        } else {
            carHolder.tv_car_juli.setText(Location2Util.getInstance(context).distance(lat, lng, car.getLat(), car.getLng()) + "km");
        }
        carHolder.tv_car_ad.setText(car.getAddress());

		return convertView;
	}
	
	public static String formatDouble(String text) {

		String format;
		try {
			DecimalFormat df = new DecimalFormat("0.0");
			double d = Double.parseDouble(text)/1000;
			format = df.format(d);
			return format;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "0";
	}



    static class CarViewHolder{

        public TextView tv_name;
        public  TextView tv_num;
        public TextView tv_wangfan;
        public TextView tv_zhifa;
        public ImageView iv_picture;
        public TextView tv_car_type;
        public  TextView tv_car_long;
        public   TextView tv_time;
        public   TextView tv_car_juli;
        public   TextView tv_car_ad;

        public CarViewHolder(View convertView) {
            tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            tv_wangfan = (TextView) convertView.findViewById(R.id.tv_wangfan);
            tv_zhifa = (TextView) convertView.findViewById(R.id.tv_zhifa);
            iv_picture = (ImageView) convertView.findViewById(R.id.iv_picture);
            tv_car_type = (TextView) convertView.findViewById(R.id.tv_car_type);
            tv_car_long = (TextView) convertView.findViewById(R.id.tv_car_long);
            tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            tv_car_juli = (TextView) convertView.findViewById(R.id.tv_car_juli);
            tv_car_ad = (TextView) convertView.findViewById(R.id.tv_car_ad);
            convertView.setTag(this);
        }

        public static CarViewHolder getHolder(View convertView){
            return (CarViewHolder) (convertView.getTag() != null ? convertView.getTag() : new CarViewHolder(convertView));
        }
    }
	
}
